package OperationSystem.day1205File;

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;

public class Traversal_All_Files {//遍历输出所有的文件

    public static void main(String[] args) {
        String path = "C:\\Users\\Lynn\\IdeaProjects\\JavaWeb\\src\\OperationSystem";
        File root = new File(path);

        //deepFirstTraversal(root);
        lengthFirstTraversal(root);
    }

    //深度优先遍历
    private static void deepFirstTraversal(File root) {
        System.out.println(root.getAbsolutePath());
        //root不是文件夹或者root为空文件夹--->叶子结点
        if (root.isFile()) {//是文件
            return;
        }
        if (!root.isDirectory()) {//不是文件夹
            return;
        }
        File[] files = root.listFiles();
        if (files == null) {
            return;
        }
        if (files.length == 0) {
            return;
        }
        for (File file : files) {
            deepFirstTraversal(file);
        }
    }


    //广度优先遍历
    private static void lengthFirstTraversal(File root) {
        Queue<File> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            File file = queue.poll();
            System.out.println(file.getAbsolutePath());
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null) {
                    for (File f : files) {
                        queue.offer(f);
                    }
                }
            }
        }
    }
}
